﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Environment;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_tacticalset officer_tacticalset(long tacticaltypecode)
        {
            string userCode = SangoManager.Authentication();

            officer_tacticalset response = ReflectionManager.CreatePojo<officer_tacticalset>();
            response.Succeed = false;

            List<Entities.Usr_Tactical> tacticals = TacticalDao.getUserTacticals(userCode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                foreach (Entities.Usr_Tactical tactical in tacticals)
                {
                    if (SettingHelper.BooleanValue(tactical.Isdefault))
                    {
                        tactical.Isdefault = SettingHelper.IntValue(false);
                        command.Update(tactical);
                    }
                }

                Entities.Usr_Tactical orm = TacticalDao.searchUsrTactical(tacticals, tacticaltypecode);

                orm.Isdefault = SettingHelper.IntValue(true);

                command.Update(orm);

                command.Commit();
            });

            List<Entities.Usr_Strategy> strategies = StrategyDao.getStrategies(userCode);
            List<player_tactical> tlist = new List<player_tactical>();
            foreach (Entities.Usr_Tactical ormtactical in tacticals)
            {
                Entities.Sys_Tactical sysTactical = SysTacticalHelper.search(ormtactical.Tacticaltypecode);
                Entities.Usr_Strategy strategy = StrategyDao.searchStrategy(strategies, sysTactical.Strategytypecode);
                tlist.Add(UsrTacticalFacade.ToTactical(
                    strategy,
                    ormtactical,
                    TacticalDao.getUserTacticalPositions(ormtactical.Primarycode)));
            }

            response.tacticals = tlist;
            response.Succeed = true;
            return response;
        }
    }

    public interface officer_tacticalset : ISangoResponse
    {
        List<player_tactical> tacticals { get;set;}
    }
}
